package ph.greenova.service.impl;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import ph.greenova.persistence.model.Person;
import ph.greenova.service.serviceinterface.PersonService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By: adrian
 * Date Created: 7:27 AM 6/7/13
 */
public class UserDetailsServiceImpl implements UserDetailsService {

    private final PersonService personService = new PersonServiceImpl();
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        Person p = personService.findPersonByUserName(s);

        if(p==null){
            throw new UsernameNotFoundException("user not found");
        }

        GrantedAuthority ga = new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return "ROLE_ADMIN";
            }
        } ;

        List<GrantedAuthority> gas = new ArrayList<GrantedAuthority>();
        gas.add(ga);
        return new User(p.getUsername(),p.getPassword(),gas);
    }
}



